/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tk.eigh.ksvg.renderer;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import ksvg.core.SVGShape;
import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
import tk.eigh.ksvg.SVG;
import tk.eigh.ksvg.component.text.AbstractTextContent;

/**
 *
 * @author nt
 */
public class SVGRendererUtils {

    protected final static Logger LOGGER = Logger.getLogger(SVGRendererUtils.class.getName());
    protected final static String ATTRIBUTES_FIELD_NAME = "INTERFACE_ATTRIBUTES";

    public static void renderImplementedAttributes(UIComponent component, ResponseWriter writer) throws IOException {
        Class clazz = component.getClass();
        while (!clazz.equals(UIComponent.class)) {
            Map<String, String> attrMap = null;
            for (Class componentClazz : clazz.getInterfaces()) {
                try {
                    attrMap = (Map<String, String>) componentClazz.getDeclaredField(ATTRIBUTES_FIELD_NAME).get(component);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(SVGRendererUtils.class.getName()).log(Level.SEVERE, null, ex);
                    attrMap = null;
                    continue;
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(SVGRendererUtils.class.getName()).log(Level.SEVERE, null, ex);
                    attrMap = null;
                    continue;
                } catch (NoSuchFieldException ex) {
                    attrMap = null;
                    continue;
                } catch (SecurityException ex) {
                    Logger.getLogger(SVGRendererUtils.class.getName()).log(Level.SEVERE, null, ex);
                    attrMap = null;
                    continue;
                } finally {
                    if (attrMap != null) {
                        for (Map.Entry<String, String> mapEntry : attrMap.entrySet()) {
                            HtmlRendererUtils.renderHTMLAttribute(writer, component, mapEntry.getKey(), mapEntry.getValue());
                        }
                    }
                }
            }
            if (clazz.equals(SVGShape.class) || clazz.equals(AbstractTextContent.class)) {
                break;
            }
            clazz = clazz.getSuperclass();
        }
    }

    public static void renderCoreAttributes(ResponseWriter writer, UIComponent component, FacesContext context) throws IOException {
        HtmlRendererUtils.writeIdIfNecessary(writer, component, context);
        for (Map.Entry<String, String> coreAttr : SVG.CORE_ATTRIBUTES_MAP.entrySet()) {
            HtmlRendererUtils.renderHTMLAttribute(writer, component, coreAttr.getKey(), coreAttr.getValue());
        }
    }
}
